//
//  RBGActualitesController.m
//  BateauGenerique
//
//  Created by Kevin De Jesus Ferreira on 24/05/2014.
//  Copyright (c) 2014 Kevin De Jesus Ferreira. All rights reserved.
//

#import "RBGActualitesController.h"
#import "RBGActualiteDetailController.h"
#import "RBGActualiteCell.h"
#import "RBGNavigationBar.h"
#import "RBGAppDelegate.h"
#import "UIImageView+WebCache.h"
#import "RBGServiceActualite.h"
#import "Actualite.h"
#import "RBGColorManager.h"

@interface RBGActualitesController ()<UITableViewDelegate,UITableViewDataSource,RBGServiceActualiteDelegate>{
    NSArray * actualites;
    RBGAppDelegate *appDelegate;
    RBGServiceActualite * serviceActualite ;
    UIRefreshControl *refreshControl;
    
    UIActivityIndicatorView *loader;
}

@end

@implementation RBGActualitesController


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self charger];
    [self chargerCouleur];
    
    UIView *refreshView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [self.tableView insertSubview:refreshView atIndex:0]; //the tableView is a IBOutlet
    
    refreshControl = [[UIRefreshControl alloc]
                                        init];
    
    refreshControl.tintColor = appDelegate.donnees.parametres.fontColorUn;

    [refreshControl addTarget:self action:@selector(rechargerActu) forControlEvents:UIControlEventValueChanged];
    [refreshView addSubview:refreshControl];
    
    loader = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    loader.frame = CGRectMake(300, 10, 20, 20);
    [self.navigationController.navigationBar.viewForBaselineLayout addSubview:loader];
    [loader startAnimating];

}

-(void) rechargerActu
{
    serviceActualite = [[RBGServiceActualite alloc]init];
    serviceActualite.delegate = self;
    [serviceActualite getActualites];

}


-(void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];
    [self chargerCouleur];
    
}

-(void)charger
{
    actualites = [[NSArray alloc]init];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    appDelegate = (RBGAppDelegate *)[[UIApplication sharedApplication] delegate];
    
    //TODO remplacer par le chargement avec url
    [self.fond setImageWithURL:[NSURL URLWithString:appDelegate.donnees.parametres.imageFond]];
    
    [self.fond setFrame:CGRectMake(0, 0, 300, 132)];
    
    serviceActualite = [[RBGServiceActualite alloc]init];
    serviceActualite.delegate = self;
    [serviceActualite getActualites];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    RBGActualiteDetailController *controller = [[RBGActualiteDetailController alloc] initWithNibName:@"RBGActualiteDetailController" bundle:nil];
    controller.actualite = [actualites objectAtIndex:indexPath.row ];
    [self.navigationController pushViewController:controller animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [actualites count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"cellActualite";
    
    Actualite * actualiteTmp = [actualites objectAtIndex:indexPath.row];
    
    RBGActualiteCell *cell = (RBGActualiteCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"cellActualite" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    cell.backgroundColor = [UIColor clearColor];
    
    UIView *selectedView = [[UIView alloc]init];
    selectedView.backgroundColor = appDelegate.donnees.parametres.backgroundColorDeux;
    cell.selectedBackgroundView =  selectedView;
   
    [cell.heure setTextColor:appDelegate.donnees.parametres.fontColorDeux];
    [cell.titreActualite setTextColor:appDelegate.donnees.parametres.fontColorDeux];
    [cell.reveil setImage:[RBGColorManager colorImageWithColor:appDelegate.donnees.parametres.fontColorDeux andImage:cell.reveil.image]];
    [cell.titreActualite setText:actualiteTmp.title];
    [cell.imageActualite setImageWithURL:[NSURL URLWithString:actualiteTmp.imageAdress]];
    [cell.heure setText:actualiteTmp.dateFormattee];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 92;
}


-(void)chargerCouleur
{
    [RBGNavigationBar customNavigationBar:self.navigationController andTitle:@"Actualités" andBackgroundColor:appDelegate.donnees.parametres.backgroundColorUn andTextColor:appDelegate.donnees.parametres.fontColorUn];
    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundView:[[UIImageView alloc] initWithImage:self.fond.image] ];
    [self.tableView setSeparatorColor:appDelegate.donnees.parametres.fontColorUn];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (void) receiveDonneesActualites:(id)donnees
                         andType :(int)typeDonnees
{
    
    NSLog(@"%@",donnees);
    actualites = (NSArray *) donnees;
    
    [self.tableView reloadData];
    [refreshControl endRefreshing];
    
    [loader stopAnimating];
    loader.hidden = YES;

}

- (void) receiveErreurActualites:(int)statut{
    // On fait quoi en cas de défaite
    [refreshControl endRefreshing];
    
    [loader stopAnimating];
    loader.hidden = YES;
    
    UIAlertView *alert = [[UIAlertView alloc]
                          
                          initWithTitle:@"Erreur"
                          message:@"Erreur réseau : votre téléphone ne peut pas accéder aux informations"
                          delegate:self
                          cancelButtonTitle:@"Ok"
                          otherButtonTitles:nil];
    
    [alert show];

}

@end
